package com.saic.localfileloader.main;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Main {

	int cnt = 0;

	public static void main(String[] args) throws IOException {

		Main main = new Main();

		int cnt = 0;
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream("test.txt")));
		String line = null;
		Map<String, String> map = new HashMap<String, String>();
		while (null != (line = br.readLine())) {
			String[] tmp = line.split(":");
			map.put(tmp[0], tmp[1]);
			cnt++;
			if (cnt % 10 == 0) {// 执行操作
				main.fun(map);
				map.clear();
			}
		}
		main.fun(map);
		map.clear();
		map = null;
		br.close();
	}

	public void fun(Map<String, String> map) {

		StringBuffer sb_key = new StringBuffer();
		StringBuffer sb_val = new StringBuffer();
		StringBuffer sb_col = new StringBuffer();
		for (String key : map.keySet()) {
			sb_key.append(key + ":" + map.get(key) + ",");
			sb_val.append(key + ",");
			sb_col.append("$\"" + key + "\",");
		}
		// pint case class MSG
		String key = sb_key.toString().substring(0, sb_key.length() - 1);
		System.out.println("case class ID_" + cnt + "(" + key
				+ ") extends Serializable");
		String value = sb_val.toString().substring(0, sb_val.length() - 1);
		System.out.println("def udf_" + cnt + "=udf((" + key + ") => ID_" + cnt
				+ "(" + value + "))");
		System.out.print(".withColumn(\"ID_" + cnt + "\",udf_" + cnt + "("
				+ sb_col.substring(0, sb_col.length() - 1) + "))");
		for (String kk : map.keySet()) {
			System.out.print(".drop(\"" + kk + "\")");
		}
		System.out.println("\n\n");

		sb_key = null;
		sb_val = null;
		sb_col = null;

		cnt++;
	}
}
